package qlttn.web.bean;

import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;

import qlttn.business.UserBO;
import qlttn.business.UserBOImpl;
import qlttn.common.utility.LoggerUtility;
import qlttn.common.valueobjects.UserVO;

@ManagedBean(name = "userBean")
@SessionScoped
public class UserBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String userName;
	private String password;
	private String message;
	private UserVO userVO;
	private UserBO userBO = null;
	private boolean isLoggedIn;
	public ArrayList<UserVO> list;

	/**
	 * Check userName and password have exited in the database.
	 * 
	 * @return logon.xhtml
	 */
	public String logon() {
		LoggerUtility.logger("UserBean.logon()..............start");
		userBO = new UserBOImpl();
		userVO = new UserVO();
		userVO.setUserName(userName);
		userVO.setPassword(password);
		try {
			if (userBO.logon(userVO)) {
				isLoggedIn = true;
				return "index";
			}
		} catch (SQLException sql) {
		} catch (ClassNotFoundException e) {
		}
		setMessage("Wrong userName/Password. Please check agagin!");
		LoggerUtility.logger("UserBean.logon()..............end");
		return "logon";
	}

	public String logout() {
		isLoggedIn = false;
		return "index";
	}

	public String profile() {
		LoggerUtility.logger("UserBean.showProfile()..............start");
		try {
			userBO = new UserBOImpl();
			userVO = userBO.showProfile(userName);
		} catch (SQLException sql) {
		} catch (ClassNotFoundException e) {
		}
		LoggerUtility.logger("UserBean.showProfile()..............end");
		return "profileUser";
	}


	/**
	 * Method for redirecting a request
	 * 
	 * @param event
	 */
	public void verifyLogon(ComponentSystemEvent event) {
		LoggerUtility.logger("UserBean.verifyLogon()..............start");
		if (!isLoggedIn) {
			try {
				FacesContext context = FacesContext.getCurrentInstance();
				context.getExternalContext().redirect("logon.jsf");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		LoggerUtility.logger("UserBean.verifyLogon()..............end");
	}

	/**
	 * Get all User
	 * @return ArrayList<UserVO>
	 */
	public ArrayList<UserVO> getList() {
		LoggerUtility.logger("UserBean.getListUsers()..............start");
		userBO = new UserBOImpl();
		try {
			list = userBO.getListUsers("1");
		} catch (SQLException sql) {
		} catch (ClassNotFoundException e) {
		}
		LoggerUtility.logger("UserBean.getListUsers()..............end");
		return list;
	}

	public void setList(ArrayList<UserVO> list) {
		this.list = list;
	}
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public boolean isLoggedIn() {
		return isLoggedIn;
	}

	public void setLoggedIn(boolean isLoggedIn) {
		this.isLoggedIn = isLoggedIn;
	}

	public UserVO getUserVO() {
		return userVO;
	}

	public void setUserVO(UserVO userVO) {
		this.userVO = userVO;
	}

}
